import { addHospitalSet, getHospitalID, updateHospitalID } from '@/api/hospital/HospitalSet'
import { HospitalItem, addHospital } from '@/api/hospital/model/HospitalSetType'
import { Button, Card, Form, Input, message } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import React, { useEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'

export default function HospitalAddOrUpdate() {
let navigate =     useNavigate()
  let [from] =   useForm()
//   存储ID获取的数据
//   获取Id
 let {id} =  useParams()
  // 添加数据
  let finish = async ()=>{
    let obj = from.getFieldsValue()
    if(id){
        await updateHospitalID({ ...obj, id })
        message.success("修改成功")

    }else{
        await  addHospitalSet(obj)
        message.success("添加成功")
        
    }
    // await  addHospitalSet(obj)
    // message.success("添加成功")

    navigate("/syt/hospital/hospitalList")
}
 let getID =async ()=>{
  let res = await  getHospitalID(id as string)
  from.setFieldsValue(res)
 }

//  模拟生命周期
useEffect(()=>{
    getID()
},[])

   
    return (
        <Card> <Form
        onFinish={finish}
        form={from}

            labelCol={{ span: 2 }}
        // wrapperCol={{ span: 16 }}
        >
            <Form.Item
                label="医院名称"
                name="hosname"
                rules={[{ required: true, message: '请输入医院名称!' }]}
            >
                <Input />
            </Form.Item>

            <Form.Item
                label="医院编号"
                name="hoscode"
                rules={[{ required: true, message: '请输入医院编号!' }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="api基础路径"
                name="apiUrl"
                rules={[{ required: true, message: '请输入api基础路径!' }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="联系人姓名"
                name="contactsName"
                rules={[{ required: true, message: '请输入联系人姓名  !' },
                { pattern: /^[\u4e00-\u9fa5]{0,}$/, message: "请输入正确的联系人名字" }
                ]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="联系人手机"
                name="contactsPhone"
                rules={[{ required: true, message: '请输入联系人手机号!' },
                { pattern: /^1([358][0-9]|4[456789]|6[67]|7[0135678]|9[1589])\d{8}$/, message: "请输入正确的手机号" }

                ]}
            >
                <Input />
            </Form.Item>
            <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                <Button type="primary" htmlType="submit" >
                    保存
                </Button>
                <Button type="default" className='ml'>
                    清空
                </Button>
            </Form.Item>
        </Form></Card>
    )
}
